package com.hitqz.robot.biz.flow.component.oilSample;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.camera.dto.SidesWayDto;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author xupkun
 * @date 2024/10/4
 */
@LiteflowComponent(name = "[取油样机器人]上装微调", value = "AdjustCarModuleComponent")
@RequiredArgsConstructor
@Slf4j
public class AdjustCarModuleComponent extends NodeComponent {

    private final RobotEquipmentService robotEquipmentService;

    private final RobotDriverService robotDriverService;

    private final EquipmentFactory equipmentFactory;

    @Override
    public void process() throws Exception {
        List<RobotEquipment> carPlc = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                //.eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
                .eq(RobotEquipment::getType, EquipmentEnum.CAR_PLC.getType())
                .eq(RobotEquipment::getBrand, BrandEnum.CAR_PLC.getEnStr())
                .eq(RobotEquipment::getIsRecordParam, 1));
        RobotEquipment robotEquipment = carPlc.get(0);
        QueryParamsDto queryParamsDto = new QueryParamsDto();
        queryParamsDto.setPassword(robotEquipment.getPassword());
        queryParamsDto.setUsername(robotEquipment.getUsername());
        queryParamsDto.setPort(robotEquipment.getPort());
        queryParamsDto.setChannel(robotEquipment.getChannel());
        queryParamsDto.setIp(robotEquipment.getIp());
        IRobotPosDto pos = robotDriverService.getWebPos();
        String qrCodeX =pos.getQrCodeX();
        Integer x = Integer.parseInt(qrCodeX);
        //Integer target = -14;
        while (true) {
            //if ((x < 0 && target <= 0)) {
            //    x = -(Math.abs(x) - Math.abs(target));
            //} else {
            //    x = x - target;
            //}
            if (x.equals(0)) {
                log.info("x== {} 跳出调整", 0);
                break;
            }
            // 每次运动
            IEquipmentService service = equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
            SidesWayDto sidesWayDto = new SidesWayDto();
            sidesWayDto.setDistance(-x);
            service.executeCmd(queryParamsDto , true , EquipmentCmdEnum.ENABLE_SIDES, sidesWayDto);
            TimeUnit.SECONDS.sleep(3);
            try {
                pos = robotDriverService.getWebPos();
                qrCodeX =pos.getQrCodeX();
                x = Integer.parseInt(qrCodeX);
            }catch (Exception e) {
                log.info("Failed to parse" + e.getMessage());
            }
            log.info("调整后再次读取的x:" + x);
        }
    }
}
